function sortStrings(arr) {
    return arr.sort((a, b) => {
        return a.localeCompare(b);
    });
}

function updateImagesIfNeed(data) {
    let allGeneratedImages = [];
    let imagesElements = $('.generated-images');
    for (let image of imagesElements) {
        allGeneratedImages.push($(image).data('src'));
    }
    sortStrings(allGeneratedImages);

    let imageListPoint = $('#image');
    for (let image of data) {
        if (!allGeneratedImages.includes(image)) {
            let newImgElement = $('<img>');
            newImgElement.attr('src', `/images/${image}.png`);
            newImgElement.attr('alt', image);
            newImgElement.addClass('generated-images');
            newImgElement.data('src', image);
            imageListPoint.prepend(newImgElement);
        }
    }
}

let QUEUE_INFO = '';
function updateQueueInfoIfNeed(data) {
    if (data !== QUEUE_INFO) {
        $('#queue-info').html(data);
        QUEUE_INFO = data;
    }
}

function init() {
    $.get('/get-images-and-queue-info', function (data) {
        updateImagesIfNeed(data.info.images);
        updateQueueInfoIfNeed(data.info.queueInfo);
        setTimeout(init, 2000);
        SUBMIT_LOCK = false;
    }).fail(function() {
        setTimeout(init, 2000);
        SUBMIT_LOCK = false;
    });
}

function getInputs(checkPrompt) {
    let prompt = $('#text-input').val();
    if (checkPrompt && !promptIsAllowed(prompt)) {
        alert('提示词不能出现中文或特殊符号，只支持英文和半角字符。');
        return;
    }

    let width = parseInt($('#width-input').val());
    let height = parseInt($('#height-input').val());
    let steps = parseInt($('#steps-input').val());
    let seed = parseInt($('#seed-input').val());
    if (prompt.length === 0) {
        alert('提示词不能为空');
        return false;
    }
    return {
        prompt: prompt,
        width: width,
        height: height,
        steps: steps,
        seed: seed,
        norwkv: 0
    };
}

let SUBMIT_LOCK = false;

$(function () {
    $('#seed-input').attr('value', Math.ceil(Math.random() * 4294967295));
    init();

    $('#submit-data').on('click', function () {
        let inputs = getInputs(true);
        if (false === inputs) {
            return;
        }
        if (SUBMIT_LOCK) {
            return;
        }
        SUBMIT_LOCK = true;

        inputs.norwkv = 1;
        $.post('/submits', {
            submits: [inputs],
        }, function (data) {
            if (data.status == 0) {
                alert('提交成功。');
                SUBMIT_LOCK = false;
            }
        }, 'json').fail(function() {
            alert('提交失败。');
            SUBMIT_LOCK = false;
        });
    });

    $('#submit-data-zh').on('click', function () {
        let inputs = getInputs(false);
        if (false === inputs) {
            return;
        }
        if (SUBMIT_LOCK) {
            return;
        }
        SUBMIT_LOCK = true;

        $.post('/submits', {
            submits: [inputs],
        }, function (data) {
            if (data.status == 0) {
                alert('提交成功。');
                SUBMIT_LOCK = false;
            }
        }, 'json').fail(function() {
            alert('提交失败。');
            SUBMIT_LOCK = false;
        });
    });
});
